local opts = { noremap = true, silent = true }

-- local term_opts = { silent = true }

-- Shorten function name
local keymap = vim.api.nvim_set_keymap

--Remap space as leader key
keymap("", " ", "<Nop>", opts)
vim.g.mapleader = " "
vim.g.maplocalleader = " "

-- Modes normal_mode = "n",
--   insert_mode = "i",
--   visual_mode = "v",
--   visual_block_mode = "x",
--   term_mode = "t", command_mode = "c",

keymap("", "i", "k", opts)
keymap("", "k", "j", opts)
keymap("", "j", "h", opts)
keymap("", "h", "i", opts)

keymap("", "I", "8k", opts)
keymap("", "K", "8j", opts)
keymap("", "J", "8h", opts)
keymap("", "L", "8l", opts)

keymap("", "H", "I", opts)

keymap("n", "<LEADER><CR>", "o<Esc>", opts)

keymap("n", "sl", ":set splitright<CR>:vsplit<CR>", opts)
keymap("n", "sj", ":set nosplitright<CR>:vsplit<CR>", opts)
keymap("n", "si", "set nosplitbelow<CR>:split<CR>", opts)
keymap("n", "sk", ":set splitbelow<CR>:split<CR>", opts)

keymap("n", "<C-j>", "<C-w>h", opts)
keymap("n", "<C-k>", "<C-w>j", opts)
keymap("n", "<C-i>", "<C-w>k", opts)
keymap("n", "<C-l>", "<C-w>l", opts)
-- -- NOTE: require winshit plugin
keymap("n", "<C-w>m", ":WinShift<cr>", opts)

-- i j remap to gi gj to get the consistent behaviour in wrap content mode (i.e. set wrap)
-- keymap("n", "j", "gj", opts)
-- keymap("n", "k", "gk", opts)

-- 写完跳出括号外
keymap("i", "jl", "<ESC>la", opts)
-- FileExpoler
keymap("n", "tt", ":NvimTreeToggle<cr>", opts)
keymap("n", "<leader>f", ":NvimTreeFindFile<cr>", opts)
-- no highlight
keymap("n", "nh", ":nohl<cr>", opts)
-- save buffer
keymap("n", "S", ":w<cr>", opts)
-- exit cur window
keymap("n", "Q", ":q<cr>", opts)
-- delete cur buffer
keymap("n", "<leader>d", ":Bdelete<cr>", opts)
keymap("n", "<leader>D", ":Bdelete<cr>", opts)
-- exit whole program
keymap("n", "ZZ", ":lua require('utils').SaveAndExit()<cr>", opts)
-- remap macro record key
--keymap("n", "Q", "q", opts)
-- cancel q
--keymap("n", "q", "<Nop>", opts)

-- center cursor
--keymap("n", "n", "nzzzv", opts)
--keymap("n", "N", "Nzzzv", opts)
--keymap("n", "J", "mzJ`z", opts)
-- keymap("n", "j", "jzz", opts)
-- keymap("n", "k", "kzz", opts)

-- Resize with arrows
keymap("n", "<C-Up>", ":resize -2<CR>", opts)
keymap("n", "<C-Down>", ":resize +2<CR>", opts)
keymap("n", "<C-Left>", ":vertical resize -2<CR>", opts)
keymap("n", "<C-Right>", ":vertical resize +2<CR>", opts)

-- Navigate buffers
-- keymap("n", "R", ":bnext<CR>", opts)
-- keymap("n", "E", ":bprevious<CR>", opts)
-- NOTE: E/R navigation needs  'bufferline' plugin
keymap("n", "tl", ":BufferLineCycleNext<CR>", opts)
keymap("n", "tj", ":BufferLineCyclePrev<CR>", opts)
keymap("n", "tml", ":BufferLineMoveNext<CR>", opts)
keymap("n", "tmj", ":BufferLineMovePrev<CR>", opts)


-- Move text up and down
--keymap("n", "<A-j>", "<Esc>:m .+1<CR>==gi", opts)
--keymap("n", "<A-k>", "<Esc>:m .-2<CR>==gi", opts)

-- Insert --
-- Press jl fast to enter
-- keymap("i", "jl", "<ESC>", opts)

-- Visual --
-- Stay in indent mode
keymap("v", "<", "<gv", opts)
keymap("v", ">", ">gv", opts)

-- Move text up and down
-- keymap("v", "<A-j>", ":m .+1<CR>==", opts)
-- keymap("v", "<A-k>", ":m .-2<CR>==", opts)
--keymap("v", "p", '"_dP', opts)

-- Visual Block --
-- Move text up and down
-- keymap("x", "J", ":move '>+1<CR>gv-gv", opts)
-- keymap("x", "K", ":move '<-2<CR>gv-gv", opts)
-- keymap("x", "<A-j>", ":move '>+1<CR>gv-gv", opts)
-- keymap("x", "<A-k>", ":move '<-2<CR>gv-gv", opts)

-- Terminal --
-- Better terminal navigation
-- keymap("t", "<C-j>", "<C-\\><C-N><C-w>h", term_opts)
-- keymap("t", "<C-k>", "<C-\\><C-N><C-w>j", term_opts)
-- keymap("t", "<C-i>", "<C-\\><C-N><C-w>k", term_opts)
-- keymap("t", "<C-l>", "<C-\\><C-N><C-w>l", term_opts)


-- keymap("n", "<leader>f", "<cmd>Telescope find_files<cr>", opts)
-- keymap("n", "<Space>f", "<cmd>lua require'telescope.builtin'.find_files(require('telescope.themes').get_dropdown({ previewer = false }))<cr>", opts)
-- keymap("n", "<Space>f", "<cmd>lua require('telescope').extensions.frecency.frecency(require('telescope.themes').get_dropdown({ previewer = false }))<cr>", opts)
-- keymap("n", "<Space>F", "<cmd>Telescope live_grep<cr>", opts)

-- bookmark's
--keymap("n", "ma", "<cmd>Telescope vim_bookmarks current_file<cr>", opts)
--keymap("n", "mA", "<cmd>Telescope vim_bookmarks all<cr>", opts)


--keymap("n", "<A-o>", "<cmd>ClangdSwitchSourceHeader<cr>", opts)
--keymap("n", "<leader>u", "<cmd>Trouble lsp_references<cr>", opts)

-- calltree
--keymap("n", "<leader>in", "<cmd>lua vim.lsp.buf.incoming_calls()<cr>", opts)

-- debug
--keymap("n", "<leader>db", "<cmd>lua require'dap'.toggle_breakpoint()<cr>", opts)
--keymap("n", "<leader>dB", "<cmd>lua require'dap'.set_breakpoint(vim.fn.input '[Condition] > ')<cr>", opts)
-- keymap("n", "<leader>dr", "lua require'dap'.repl.open()<cr>", opts)
--keymap("n", "<leader>dl", "lua require'dap'.run_last()<cr>", opts)
--keymap('n', '<F10>', '<cmd>lua require"user.dap.dap-util".reload_continue()<CR>', opts)
--keymap("n", "<F4>", "<cmd>lua require'dap'.terminate()<cr>", opts)
--keymap("n", "<F5>", "<cmd>lua require'dap'.continue()<cr>", opts)
--keymap("n", "<F6>", "<cmd>lua require'dap'.step_over()<cr>", opts)
--keymap("n", "<F7>", "<cmd>lua require'dap'.step_into()<cr>", opts)
--keymap("n", "<F8>", "<cmd>lua require'dap'.step_out()<cr>", opts)
--keymap("n", "K", "<cmd>lua require'dapui'.eval()<cr>", opts)
-- keymap("n", "<leader>dt", "<cmd>lua require'dapui'.toggle()<cr>", opts)
-- keymap("n", "<leader>dx", "<cmd>lua require'dap'.terminate()<cr>", opts)

-- git diff view
--keymap('n', '<leader>k', ']c', opts)
--keymap('n', '<leader>i','[c', opts)
--
--
---- unit test
--keymap("n", "<leader>rr", "<cmd>UltestNearest<cr>", opts)
--keymap("n", "<leader>rd", "<cmd>UltestDebugNearest<cr>", opts)
--
---- comment
--keymap("n", "gcf", "<cmd>Dox<cr>", opts)
--
---- sniprun
--keymap("n", "<leader>rf", ":%SnipRun<cr>", opts)
--keymap("v", "<leader>rs", ":%SnipRun<cr>", opts)

keymap("n", "<C-t>", "<cmd>AerialToggle!<CR>", opts)
keymap("n", "gs", ":Switch<CR>", opts)
keymap("n", "<leader>td", "<cmd>TodoTelescope theme=dropdown<CR>", opts)



-- 全项目替换
keymap("n", "<leader>rp", "<cmd>lua require('spectre').open()<CR>", opts)
-- 只替换当前文件
keymap("n", "<leader>rf", "<cmd>:lua require('spectre').open_file_search()<CR>", opts)
-- 全项目中搜索当前单词
keymap("n", "<leader>rw", "<cmd>lua require('spectre').open_visual({select_word=true})<CR>", opts)

-- for spell check
vim.cmd(
[[
  nnoremap <leader>s :call search('\w\>', 'c')<CR>a<C-X><C-S>
]])
